/**
 * Copyright (c) 2019-2021,  陈俊兵 (cjb6b.com@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ktjiaoyu.thread.create.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 *
 * @packageName com.ktjiaoyu.thread.create.callable
 * @fileName MyCallable.java
 * @author 陈俊兵 (cjb6b.com@gmail.com)
 * @createTime 2023/7/20 17:34
 * @lastModify 2023/7/20 17:34
 * @version 1.0.0
 */
public class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            System.out.println(Thread.currentThread().getName() + "正在打"+ (i + 1) + "颗螺丝！");
        }
        return Thread.currentThread().getName() + "打完螺丝了";
    }

    public static void main(String[] args) {
        FutureTask<String> task = new FutureTask<>(new MyCallable());
        // 传递任务
        Thread thread = new Thread(task, "王五");
        // 启动任务
        thread.start();

        Thread.currentThread().setName("包工头");
        for (int i = 0; i < 100; i++) {
            Thread.yield();
            System.out.println(Thread.currentThread().getName() + "正在抽烟！");
        }

        try {
            System.out.println("task.isDone() ：" + task.isDone() );
            String result = task.get();
            System.out.println("task.isDone() ：" + task.isDone() );
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
